package core;


import semanticAbstraction.SemanticHandlerFactory;

import semanticAbstraction.interfaces.SemanticHandler;

import helper.stringHelper.StringHelper;

import java.io.IOException;
import java.util.HashMap;

import helper.GroundingInformation;
import helper.wsdlHelper.WSDLHelper;




import core.exceptions.InvocationException;
import core.interfaces.CoreInterface;



/**
 * This class implements the core functionality of SISi by means of {@link CoreInterface}. 
 * It is the basis for all {@ link externalInterface}s.
 * 
 * @author Christian Stamber
 *
 */
public class CoreController implements CoreInterface{
	
	public String invokeSemantically(String semanticDescription, String semanticInput){
		System.out.println("invokeSemantically("+ semanticDescription+", "+semanticInput+")");
		SemanticHandler sh;
		sh=SemanticHandlerFactory.createSemanticHandler(semanticDescription);
		
		String result="FAULT";
		
		try {
			System.out.println(" ------------------ RESULT ---------------------");
			result=sh.invokeSemantically(semanticDescription, semanticInput);
			System.out.println(result);
			System.out.println(" ------------------ /RESULT --------------------");
			
		} catch (Exception e) {
			e.printStackTrace();
			result="EXCEPTION DURING INVOKESEMANTICALLY:\n"+e.getMessage();
			//throw new InvocationException();
		}
		
		return result;
		
	}
	
	public String invokeSyntactically(String semanticDescription, String soapBodyInput){
		System.out.println("invokeSyntactically("+ semanticDescription+", "+soapBodyInput+")");
		SemanticHandler sh;
		sh=SemanticHandlerFactory.createSemanticHandler(semanticDescription);
		
		String result;
		
		try {
			GroundingInformation grounding;
			grounding=sh.discoverWebService(semanticDescription);
			
			result=WSDLHelper.invokeWebService(	grounding.getWsdlLocation(), 
												grounding.getPort(), 
												grounding.getOperation(),
												soapBodyInput);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			//throw new InvocationException();
		}
		return "";
	}
	
	/*
	public HashMap invokeSyntactically(String semanticDescription, HashMap inputData) throws Exception{
		SemanticHandler sh;
		sh=SemanticHandlerFactory.createSemanticHandler(semanticDescription);
		
		
		String[] inputArray;
		inputArray=(String[])inputData.values().toArray(new String[0]);
		
		GroundingInformation grounding;
		HashMap output;
		
		
		grounding=sh.discoverWebService(semanticDescription);

		output=WSDLHelper.invokeWebService(	grounding.getWsdlLocation(), 
											grounding.getPort(), 
											grounding.getOperation(),
											inputArray);

		return output;
	}*/
	
	
	public boolean decide(String semanticInput, String condition) {
		System.out.println("decide("+ semanticInput+", "+condition+")");
		SemanticHandler sh;
		sh=SemanticHandlerFactory.createSemanticHandler(semanticInput);
		
		boolean result;
		result=sh.decide(semanticInput, condition);
		
		return result;
	}
	
	public String map(String sourceGoal, String sourceInstance, String targetGoal, String targetInstance, String sourceInput){
		System.out.println("map("+ sourceGoal+", "+ sourceInstance+", "+ targetGoal+", "+ targetInstance+", "+sourceInput+")");
		SemanticHandler sh;
		sh=SemanticHandlerFactory.createSemanticHandler(sourceInput);
		
		String result;
		
		result=sh.map(sourceGoal, sourceInstance, targetGoal, targetInstance, sourceInput);
		
		return result;
	}
	
	public String merge(String input1, String ontologyName1, String instanceName1, String input2, String ontologyName2, String instanceName2, String outputOntologyName){
		System.out.println("merge("+ input1+", "+ ontologyName1+", "+ instanceName1+", "+ input2+", "+ontologyName2+", "+ instanceName2+", "+outputOntologyName+")");
		SemanticHandler sh;
		sh=SemanticHandlerFactory.createSemanticHandler(input1.concat(input2));
		
		String result;
		
		result=sh.merge(input1, ontologyName1, instanceName1, input2, ontologyName2, instanceName2, outputOntologyName);
		
		return result;
	}
	

	
}
